package com.cskaoyan.wx.controller;

import com.cskaoyan.bean.Goods;
import com.cskaoyan.service.CategoryService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.wx.service.GoodsService;
import com.cskaoyan.wx.vo.GoodsCategoryVO;
import com.cskaoyan.wx.vo.GoodsCountVO;
import com.cskaoyan.wx.vo.GoodsDetailVO;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("wx/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    CategoryService categoryService;

    @GetMapping("count")
    public BaseRespVo count() {
        int count = goodsService.getGoodsCount();

        return BaseRespVo.ok(new GoodsCountVO(count));
    }

    @GetMapping("category")
    public BaseRespVo category(int id) {
        GoodsCategoryVO categoryVO = categoryService.getGoodsCategory(id);


        return BaseRespVo.ok(categoryVO);
    }

    @GetMapping("list")
    public BaseRespVo list(String keyword, String sort, String order, Integer page, Integer size, Integer categoryId, Integer brandId, Boolean isNew, Boolean isHot) {
        HashMap<String, List<Goods>> map = new HashMap<>();
        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");
        List<Goods> goods = categoryService.getGoods(keyword, sort, order, categoryId, page, size, username,brandId,isNew,isHot);
        if (goods != null) {
            map.put("goodsList", goods);
            return BaseRespVo.ok(map);
        }else {
            return BaseRespVo.fail("系统内部错误",502);
        }
    }

    @GetMapping("detail")
    public BaseRespVo detail(int id, HttpSession session) {
        GoodsDetailVO detailVO = goodsService.getDetail(id);

        return BaseRespVo.ok(detailVO);
    }

    @GetMapping("related")
    public BaseRespVo related(int id) {
        List<Goods> goods = goodsService.getRelated(id);
        HashMap<String, List<Goods>> map = new HashMap<>();
        map.put("goodsList", goods);
        return BaseRespVo.ok(map);
    }
}
